package siga.visao_academico;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;
import siga.visao_busca.buscaTurma;


public class GestaoTurmaProfessor extends javax.swing.JInternalFrame {
    private String cod_curso="";
    List cod_disciplina = new ArrayList();
    public GestaoTurmaProfessor() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jtfDescricao = new javax.swing.JTextField();
        jbBusca = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jlTurma = new javax.swing.JLabel();
        jlFase = new javax.swing.JLabel();
        jlFase1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Vínculo de Disciplina do Professor");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton2.setText("Bucar Turma");
        jButton2.setToolTipText("Busca somente Acadêmicos");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/canelar_matricula_32.png"))); // NOI18N
        jButton3.setText("Fechar Edição");
        jButton3.setEnabled(false);
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        btSair.setMnemonic('S');
        btSair.setText("Sair");
        btSair.setToolTipText("Sair da Janela");
        btSair.setFocusable(false);
        btSair.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSair.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSair.setName("btSair"); // NOI18N
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        jToolBar1.add(btSair);

        jSplitPane1.setDividerLocation(600);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Descricao"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.setEnabled(false);
        jTable2.setName("jTable2"); // NOI18N
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable2);
        jTable2.getColumnModel().getColumn(0).setMinWidth(100);
        jTable2.getColumnModel().getColumn(0).setPreferredWidth(100);
        jTable2.getColumnModel().getColumn(0).setMaxWidth(100);

        jLabel3.setText("Localizar Docente:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfDescricao.setEnabled(false);
        jtfDescricao.setName("jtfDescricao"); // NOI18N
        jtfDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescricaoKeyPressed(evt);
            }
        });

        jbBusca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca.setEnabled(false);
        jbBusca.setMargin(new java.awt.Insets(1, 1, 1, 1));
        jbBusca.setName("jbBusca"); // NOI18N
        jbBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(179, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jtfDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBusca)
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbBusca))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(jPanel1);

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Disciplina", "Fase", "Docente"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setEnabled(false);
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setMinWidth(60);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(60);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(60);

        jLabel1.setText("Turma Selecionada:");
        jLabel1.setName("jLabel1"); // NOI18N

        jlTurma.setText("turma");
        jlTurma.setName("jlTurma"); // NOI18N

        jlFase.setText("fase");
        jlFase.setName("jlFase"); // NOI18N

        jlFase1.setText("/");
        jlFase1.setName("jlFase1"); // NOI18N

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/Business_People_16x16.png"))); // NOI18N
        jButton1.setToolTipText("Remover Professor");
        jButton1.setEnabled(false);
        jButton1.setMargin(new java.awt.Insets(1, 1, 1, 1));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/page_delete.png"))); // NOI18N
        jButton4.setToolTipText("Remover Duplicação");
        jButton4.setEnabled(false);
        jButton4.setMargin(new java.awt.Insets(1, 1, 1, 1));
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlTurma)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlFase1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlFase)
                .addContainerGap(356, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addContainerGap(526, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jlTurma)
                    .addComponent(jlFase1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlFase))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 928, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 928, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        index.LRturma.clear();
        index.LRfase.clear();
        buscaTurma bt = new buscaTurma(null,true);
        bt.setLocation((index.tela.width-bt.getWidth())/2, (index.tela.height-bt.getHeight())/2);
        bt.setVisible(true);
        if(buscaTurma.isConfirmaTurma()){
            try {
                jlTurma.setText(index.LRturma.get(0).toString());
                jlFase.setText(index.LRfase.get(0).toString());
                conexao.Query("select cod_curso from grade_turma where cod_emp=" + index.cod_emp + " and codigo=\"" + index.LRturma.get(0).toString() + "\"");
                conexao.rs.next();
                cod_curso = conexao.rs.getString("cod_curso").toString();
                gerarTabelaDisciplinas();
                gerenciaCampos();
                gerarTabelaProfessor();
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
            
        }
        index.LRturma.clear();
        index.LRfase.clear();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jtfDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescricaoKeyPressed
        if(evt.getKeyCode()==10){
            gerarTabelaProfessor();
        }
    }//GEN-LAST:event_jtfDescricaoKeyPressed

    private void jbBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscaActionPerformed
        gerarTabelaProfessor();
    }//GEN-LAST:event_jbBuscaActionPerformed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
        if(jTable1.isEnabled()){
            if(evt.getClickCount()==2){
                if(!jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().isEmpty()){
                    conexao.Inserir("update diario set cod_prof_t="+jTable2.getValueAt(jTable2.getSelectedRow(), 0)+" where cod_emp="+index.cod_emp+" and cod_matricula=\""+jlTurma.getText()+"\" and cod_disciplina="+cod_disciplina.get(jTable1.getSelectedRow()));
                    gerarTabelaDisciplinas();
                }else{
                    JOptionPane.showMessageDialog(null, "Disciplina é um campo obrigatório.","Alerta do Sistema",JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jTable2MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(!jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().isEmpty()){
            conexao.Inserir("update diario set cod_prof_t=0 where cod_emp="+index.cod_emp+" and cod_matricula=\""+jlTurma.getText()+"\" and cod_disciplina="+cod_disciplina.get(jTable1.getSelectedRow()));
            gerarTabelaDisciplinas();
            JOptionPane.showMessageDialog(null, "Professor Removido.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "Disciplina é um campo obrigatório.","Alerta do Sistema",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        gerenciaCampos();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        setVisible(false);
        dispose();
}//GEN-LAST:event_btSairActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
//        if(!jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().isEmpty()){
//            try {
//                conexao.Query("select codigo from diario where cod_emp="+index.cod_emp+" and cod_matricula=\""+jlTurma.getText()+"\" and cod_disciplina="+cod_disciplina.get(jTable1.getSelectedRow()));
//                conexao.rs.next();
//                String codigo = conexao.rs.getString("codigo");
//                conexao.Inserir("delete from diario_professor where codigo="+codigo+" and cod_emp="+index.cod_emp+" and cod_turma=\""+jlTurma.getText()+"\" and cod_disciplina="+cod_disciplina.get(jTable1.getSelectedRow()));
//                JOptionPane.showMessageDialog(null, "Disciplina Duplicada Removida.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
//                gerarTabelaDisciplinas();
//            } catch (SQLException ex) {
//                verErros.setErro(ex.getMessage());
//            }
//        }else{
//            JOptionPane.showMessageDialog(null, "Disciplina é um campo obrigatório.","Alerta do Sistema",JOptionPane.ERROR_MESSAGE);
//        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void gerarTabelaDisciplinas(){
        try {
            javax.swing.table.DefaultTableModel jtd = (javax.swing.table.DefaultTableModel) jTable1.getModel();
            for (int x = jTable1.getRowCount() - 1; x >= 0; --x) {
                jtd.removeRow(x);
            }
            cod_disciplina.clear();
            conexao.Query("select gc.cod_fase, gd.codigo, gd.descricao, df.cod_prof_t, p.descricao as desc_pessoa from grade_curricular gc"
                    + " inner join grade_disciplina gd on gd.codigo=gc.cod_disciplina and gd.cod_curso=gc.cod_curso and gd.cod_emp=gc.cod_emp"
                    + " left join diario df on df.cod_disciplina=gc.cod_disciplina and df.cod_emp=gc.cod_emp and df.cod_curso=gc.cod_curso"
                    + " left join pessoas p on p.cod_emp=gc.cod_emp and p.codigo=df.cod_prof_t"
                    + " where gc.cod_curso="+cod_curso+" and gc.cod_fase="+jlFase.getText()+" and df.cod_matricula=\""+jlTurma.getText()+"\" group by df.cod_disciplina order by gc.ordem");
            while (conexao.rs.next()) {
                cod_disciplina.add(conexao.rs.getString("codigo"));
                jtd.addRow(new Object[]{conexao.rs.getString("descricao"), conexao.rs.getString("cod_fase"),conexao.rs.getString("desc_pessoa")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }
    
    private void gerarTabelaProfessor(){
        String sql="";
        try {
            javax.swing.table.DefaultTableModel jtd = (javax.swing.table.DefaultTableModel) jTable2.getModel();
            for (int x = jTable2.getRowCount() - 1; x >= 0; --x) {
                jtd.removeRow(x);
            }
            if(!jtfDescricao.getText().equals("")){
                sql = " and descricao like \"%"+jtfDescricao.getText()+"%\" ";
            }
            conexao.Query("select codigo,descricao from pessoas where tipo=\'Professor\' "+sql+" and cod_emp="+index.cod_emp+" order by descricao");
            while (conexao.rs.next()) {
                jtd.addRow(new Object[]{conexao.rs.getString("codigo"), conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }
    
    private void gerenciaCampos(){
        jtfDescricao.setEnabled(!jbBusca.isEnabled());
        jButton3.setEnabled(!jbBusca.isEnabled());
        jButton1.setEnabled(!jbBusca.isEnabled());
        jTable1.setEnabled(!jbBusca.isEnabled());
        jTable2.setEnabled(!jbBusca.isEnabled());
        jButton4.setEnabled(!jbBusca.isEnabled());
        jbBusca.setEnabled(!jbBusca.isEnabled());
        jButton2.setEnabled(!jbBusca.isEnabled());
        btSair.setEnabled(!jbBusca.isEnabled());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btSair;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbBusca;
    private javax.swing.JLabel jlFase;
    private javax.swing.JLabel jlFase1;
    private javax.swing.JLabel jlTurma;
    private javax.swing.JTextField jtfDescricao;
    // End of variables declaration//GEN-END:variables
}
